/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controler;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import graph.Graph;
import graph.Node;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.text.Document;
import view.EdgeItem;
import view.MainPanel;
import view.NextPrevPanel;
import view.NodeItem;

/**
 *
 * @author borysfan
 */
public class MainPanelControler {
    private MainPanel panel;
    private Controler controler;
    private PrevNextControler prevNextControler;
    private Graph graphModel;
    private ArrayList<EdgeItemControler> edgesList = new ArrayList<EdgeItemControler>();
    

    public MainPanelControler(){
        panel = new MainPanel(this);
        graphModel = Graph.getInstance();
        initActions();
    }

    public MainPanelControler(Controler c){
        controler = c;
        panel = new MainPanel(this);
        prevNextControler = new PrevNextControler(c);
        panel.addNextPrevPanel(prevNextControler.getView());
        graphModel = Graph.getInstance();
        initActions();

    }

    public void initActions(){
        panel.setPlusBtnVisible(false);
        panel.setDropTarget(new DropTarget(panel,new DropTargetPanel()));
        panel.setPlusBtnAction(new PlusBtnAction());
        panel.setRightBtnAction(new LeftBtnAction());
        panel.setLeftBtnAction(new RightBtnAction());
        panel.setUpBtnAction(new DownBtnAction());
        panel.setDownBtnAction(new UpBtnAction());

    }

    public MainPanel getView(){
        return panel;
    }

    public PrevNextControler getPrevNextControler(){
        return prevNextControler;
    }

    public void addEdge(EdgeItemControler eic) {
        edgesList.add(eic);
        panel.updateUI();
    }

    public Graph getGraph(){
        return graphModel;
    }
    
    public ArrayList<EdgeItemControler> getEdgesList() {
        return edgesList;
    }

      public void point(Point point) {
          controler.getToolboxControler().blackNode();
        int flag=0;
        if (getEdgesList().isEmpty())
            System.out.println("Nie ma krawedzi");
        else{
            int x1=point.x;
            int y1=point.y;
            for (int i=0; i<getEdgesList().size(); i++){
                for(int j=15; j<=25; j++){
            int x2=getEdgesList().get(i).getBeginNode().getNodeItem().getX()+j;
            int y2=getEdgesList().get(i).getBeginNode().getNodeItem().getY()+j;
            int x3=getEdgesList().get(i).getEndNode().getNodeItem().getX()+j;
            int y3=getEdgesList().get(i).getEndNode().getNodeItem().getY()+j;
            int  det = x1*y2 + x2*y3 + x3*y1 -x3*y2 - x1*y3 - x2*y1;
           // System.out.println(det);
                if (det>-200 && det < 200){
                    System.out.println(x2+" "+x1+" "+x3);
                    if((x2<=x1 && x1<=x3)||(x3<=x1 && x1<=x2))
                        if((y2<=y1 && y1<=y3)||(y3<=y1 && y1<=y2)){
                            getEdgesList().get(i).showProperties();

                            flag++;
                        }
                    //System.out.println("Znalazlem");
                }
                }

            }
          }
          if (flag==0)
              controler.showEmptyProperties();

    }

 
    public Color getDefColor(EdgeItemControler itemControler) {
        if (itemControler.equals(controler.getActivEdgeControler()))
           return itemControler.getView().getColor();
        else
        return itemControler.getView().getDefColor();
    }

    public void setPlusBtnVisible(boolean b) {
        panel.setPlusBtnVisible(b);
    }

    public void deleteNode(NodeItemControler activNodeControler) {
        for (int i=0; i<activNodeControler.getModel().getEdges().size();i++)
            for (int j=0; j<edgesList.size(); j++)
        if (edgesList.get(j).getModel().equals(activNodeControler.getModel().getEdges().get(i)))
            edgesList.remove(edgesList.get(j));
        graphModel.removeNode(activNodeControler.getModel());
        panel.remove(activNodeControler.getView());
    }

  public void deleteEdge(EdgeItemControler eic){
        edgesList.remove(eic);
        for (int i=0; i<graphModel.getNodes().size(); i++)
        graphModel.getNodes().get(i).removeEdge(eic.getModel());
        controler.showEmptyProperties();
    }

    public void resetColor() {
        for (int i=0; i<graphModel.getNodes().size(); i++){
        graphModel.getNodes().get(i).setDefColor(Color.black);
        graphModel.getNodes().get(i).setColor(Color.black);
        for (int j=0; j<graphModel.getNodes().get(i).getEdges().size(); j++){
            graphModel.getNodes().get(i).getEdges().get(j).setDefColor(Color.black);
            graphModel.getNodes().get(i).getEdges().get(j).setColor(Color.black);}
    }
    }

    public void reset() {
        graphModel = new Graph();
        Component [] components = panel.getComponents();
        for(int i=0;i<components.length;i++){
            if(components[i] instanceof JButton) continue;
            if(components[i] instanceof NextPrevPanel) continue;
            panel.remove(components[i]);
        }
        edgesList = new ArrayList<EdgeItemControler>();
        panel.repaint();
    }

     public void moveLeft(){
        Component[] comps = panel.getComponents();
        if(comps!=null)
        for(int i=0;i<comps.length;i++){
            if(comps[i] instanceof NodeItem){
                NodeItem item = (NodeItem)comps[i];
                item.getControler().moveLeft();
                item.repaint();
            }
        }
    }

    public void moveRight(){
        Component[] comps = panel.getComponents();
        if(comps!=null)
        for(int i=0;i<comps.length;i++){
            if(comps[i] instanceof NodeItem){
                NodeItem item = (NodeItem)comps[i];
                item.getControler().moveRight();
                item.repaint();
            }
        }
    }

    public void moveUp(){
        Component[] comps = panel.getComponents();
        if(comps!=null)
        for(int i=0;i<comps.length;i++){
            if(comps[i] instanceof NodeItem){
                NodeItem item = (NodeItem)comps[i];
                item.getControler().moveUp();
                item.repaint();
            }
        }
    }

    public void moveDown(){
        Component[] comps = panel.getComponents();
        if(comps!=null)
        for(int i=0;i<comps.length;i++){
            if(comps[i] instanceof NodeItem){
                NodeItem item = (NodeItem)comps[i];
                item.getControler().moveDown();
                item.repaint();
            }
        }
    }
   

    public BufferedImage getWeatherImage(EdgeItemControler edge){
        int i = edge.getModel().getWeatherConditions();
        int j = edge.getModel().getWeatherSlider();
        File plik = new File("icons/sun.png");
        if (i==1){
            if(j<51)
                plik = new File("icons/rain.png");
            if(j>=51)
                plik = new File("icons/heavyrain.png");
        }
        if (i==2)
            plik = new File("icons/snow.png");
        if (i==3)
            plik = new File("icons/hail.png");
        if (i==4)
            plik = new File("icons/sleet.png");
        if (i==5)
            plik = new File("icons/fog.png");

         try {
                BufferedImage obrazek = ImageIO.read(plik);
                return obrazek;
                } catch (IOException ex) {
                    System.out.println("Problem z wszytaniem ikony");
                return null;
            }
    }

    public BufferedImage getRoadImage(EdgeItemControler edge){
        int i = edge.getModel().getRoadConditions();
        File plik = new File("icons/road.png");
  
        if (i==1)
            plik = new File("icons/roadwork.png");
        if (i==2)
            plik = new File("icons/cross.png");
        if (i==3)
            plik = new File("icons/lights.png");
 

         try {
                BufferedImage obrazek = ImageIO.read(plik);
                return obrazek;
                } catch (IOException ex) {
                    System.out.println("Problem z wczytaniem ikony");
                return null;
            }
    }

    public BufferedImage getTrafficImage(){
        File plik = new File("icons/car.png");

         try {
                BufferedImage obrazek = ImageIO.read(plik);
                return obrazek;
                } catch (IOException ex) {
                    System.out.println("Problem z wczytaniem ikony");
                return null;
            }
    }

    public BufferedImage getBlockImage(){
        File plik = new File("icons/block.png");

         try {
                BufferedImage obrazek = ImageIO.read(plik);
                return obrazek;
                } catch (IOException ex) {
                    System.out.println("Problem z wczytaniem ikony");
                return null;
            }
    }

    public boolean showIcons() {
        return controler.getShowIcons();
    }

    public void addNode(NodeItem view){
        panel.add(view);
        panel.repaint();
    }

    public void addNode(NodeItemControler  nodeItem){
         nodeItem.setToolTip("");
         nodeItem.removeMouseListener();
         nodeItem.addMouseMotions();
         panel.add(nodeItem.getView());
         panel.repaint();
    }

    public void setPrevNextPanelVisible(boolean visible){
        panel.setPrevNextPanelVisible(visible);
    }


    private class DropTargetPanel implements DropTargetListener{
        public void dragEnter(DropTargetDragEvent dtde) {}

        public void dragOver(DropTargetDragEvent dtde) {}

        public void dropActionChanged(DropTargetDragEvent dtde) {}

        public void dragExit(DropTargetEvent dte) {}
        public void drop(DropTargetDropEvent dtde) {
           controler.getToolboxControler().blackNode();
           NodeItemControler nodeItem = new NodeItemControler(controler);
           nodeItem.setToolTip("");
           nodeItem.setText("wezel_"+graphModel.getId());
           nodeItem.setPosition(dtde.getLocation().x-20,+dtde.getLocation().y-20);
           nodeItem.removeMouseListener();
           nodeItem.addMouseMotions();
           Node node = nodeItem.getModel();
           graphModel.addNode(node);
           panel.add(nodeItem.getView());
           panel.repaint();
        }
    }

    private class PlusBtnAction implements ActionListener{

        public void actionPerformed(ActionEvent e) {
            ResultControler rc = controler.getResultControler();
            System.out.println("PlusBtn");
            if(rc!=null){
                rc.setVisible(true);
               
            }

        }

    }

  

    private class RightBtnAction implements ActionListener{

        public void actionPerformed(ActionEvent e) {
           moveRight();
        }

    }

     private class LeftBtnAction implements ActionListener{

        public void actionPerformed(ActionEvent e) {
           moveLeft();
        }

    }

      private class UpBtnAction implements ActionListener{

        public void actionPerformed(ActionEvent e) {
           moveUp();
        }

    }

       private class DownBtnAction implements ActionListener{

        public void actionPerformed(ActionEvent e) {
           moveDown();
        }

    }


}
